import { message } from 'antd';

interface IProduct {
  productId: string;
  productName: string;
  price: number;
  unit: string;
}

const SAVE_BY_KEY = 'SAVE_BY_KEY';

interface IActionSaveByKey {
  type: typeof SAVE_BY_KEY;
  key: string;
  payload: IProduct;
}

interface IActionTrace {
  type: 'trace';
  payload: IProduct;
}

export type ProductActionTypes = IActionSaveByKey | IActionTrace;

const initState: IProduct = {
  productId: 'vue',
  productName: 'VUE',
  price: 0,
  unit: 'copy',
};

export default {
  namespace: 'product',
  state: initState,

  effects: {},

  reducers: {
    reset() {
      return initState;
    },
    saveByKey(state: IProduct, action: IActionSaveByKey) {
      return {
        ...state,
        ...action.payload,
        // [action.key]: action.payload,
      } as IProduct;
    },
    trace(state: IProduct) {
      return state;
    },
  },
};

export { IActionSaveByKey };
